package com.benyi.util;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileTool {
	private static final String CPathFlag="classpath:";
	
	public static String readTextFile(String file) throws Exception { 
		InputStreamReader reader=null;
		StringBuilder sb=new StringBuilder();
		if(file.startsWith(CPathFlag)){
    		file=file.substring(CPathFlag.length()); 
    		ClassLoader loader=Tools.class.getClassLoader(); 
			InputStream is=loader.getResourceAsStream(file);  
			reader = new InputStreamReader(is, "UTF-8");  
    	}else{ 
    		reader = new InputStreamReader(new FileInputStream(file), "UTF-8");  
    	} 
    	int c=reader.read();
    	while(c!=-1) {
    		sb.append((char)c);
    		c=reader.read();
    	}
    	reader.close();
    	return sb.toString();
	}
	
}
